package com.weasel.security.escape.defaults;

import com.weasel.core.helper.GodHands;
import com.weasel.security.escape.EscapeService;

import java.lang.reflect.Field;

/**
 * Created by Dylan on 2015/12/18.
 */
public abstract class AbstractEscapeService implements EscapeService{

    @Override
    public <T> T escape(T obj) {

        if(null == obj){
            return null;
        }
        if(obj instanceof String){
            return (T)escapeString((String)obj);
        }

        Field [] escapeFiles = getEscapeFiles(obj);
        for(Field field : escapeFiles){
            Object value =  GodHands.getFieldValue(obj, field.getName());
            if(value instanceof String){
                String _value = escapeString((String)value);
                GodHands.setFieldValue(obj, field.getName(), _value);
            }
        }
        return obj;
    }

    protected <T> Field [] getEscapeFiles(T obj){
        return new Field[]{};
    }

    protected String escapeString(String text){
        return text;
    }
}
